package model

import (
	"common/helper"
	"database/sql"
	"errors"
	"fmt"
	g "github.com/doug-martin/goqu/v9"
)

type TblPromoInspection struct {
	ID              string  `json:"ID" db:"ID"`
	ChangeMoney     float64 `json:"ChangeMoney" db:"ChangeMoney"`         //金额变化
	NeedWageRequire float64 `json:"NeedWageRequire" db:"NeedWageRequire"` //需要的打码量
	CurWageRequire  float64 `json:"CurWageRequire" db:"CurWageRequire"`   //当前打码量
	Status          int     `json:"Status" db:"Status"`                   // 状态 0未完成 1 已完成
}
type promoInspectionData struct {
	T int                  `json:"total"`
	D []TblPromoInspection `json:"d"`
	S uint                 `json:"s"`
}

func PromoInspectionList(page, pageSize uint, uid string) (promoInspectionData, error) {

	data := promoInspectionData{}
	ex := g.Ex{}
	if uid != "" {
		ex["uid"] = uid
	}
	t := dialect.From("tbl_promo_inspection")
	if page >= 1 {
		query, _, _ := t.Select(g.COUNT(1)).Where(ex).ToSQL()

		err := meta.MerchantDB.Get(&data.T, query)
		if err != nil {
			return data, pushLog(fmt.Errorf("%s,[%s]", err.Error(), query), "数据库错误")
		}

		if data.T == 0 {
			return data, nil
		}
	}

	offset := (page - 1) * pageSize
	query, _, _ := t.Select(colsPromoInspection...).Where(ex).Offset(offset).Limit(pageSize).Order(g.C("id").Desc()).ToSQL()

	err := meta.MerchantDB.Select(&data.D, query)
	if err != nil && err != sql.ErrNoRows {
		return data, pushLog(fmt.Errorf("%s,[%s]", err.Error(), query), "数据库错误")
	}

	data.S = pageSize
	return data, nil
}

func MerchantPromoInspectionList(page, pageSize uint, uid, operatorId string) (promoInspectionData, error) {

	data := promoInspectionData{}
	ex := g.Ex{}
	uids := make([]string, 0)
	if uid != "" {
		mb, err := MemberFindByUidAndOpeId(uid, operatorId)
		if err != nil {
			return data, errors.New(helper.OperatorIdErr)
		}
		ex["uid"] = mb.Uid
	} else {
		members, err := MemberFindByOperatorId(operatorId)
		if err != nil {
			return data, errors.New(helper.OperatorIdErr)
		}
		if len(members) > 0 {
			for _, val := range members {
				uids = append(uids, val.Uid)
			}
			ex["uid"] = uids
		}
	}

	t := dialect.From("tbl_promo_inspection")
	if page >= 1 {
		query, _, _ := t.Select(g.COUNT(1)).Where(ex).ToSQL()

		err := meta.MerchantDB.Get(&data.T, query)
		if err != nil {
			return data, pushLog(fmt.Errorf("%s,[%s]", err.Error(), query), "数据库错误")
		}

		if data.T == 0 {
			return data, nil
		}
	}

	offset := (page - 1) * pageSize
	query, _, _ := t.Select(colsPromoInspection...).Where(ex).Offset(offset).Limit(pageSize).Order(g.C("id").Desc()).ToSQL()

	err := meta.MerchantDB.Select(&data.D, query)
	if err != nil && err != sql.ErrNoRows {
		return data, pushLog(fmt.Errorf("%s,[%s]", err.Error(), query), "数据库错误")
	}

	data.S = pageSize
	return data, nil
}

func BusinessPromoInspectionList(page, pageSize uint, uid, businessId string) (promoInspectionData, error) {

	data := promoInspectionData{}
	ex := g.Ex{}
	uids := make([]string, 0)
	if uid != "" {
		mb, err := MemberFindByUidAndProId(uid, businessId)
		if err != nil {
			return data, errors.New(helper.BusinessIdErr)
		}
		ex["uid"] = mb.Uid
	} else {
		members, err := MemberFindByBusinessId(businessId)
		if err != nil {
			return data, errors.New(helper.BusinessIdErr)
		}
		if len(members) > 0 {
			for _, val := range members {
				uids = append(uids, val.Uid)
			}
			ex["uid"] = uids
		}
	}

	t := dialect.From("tbl_promo_inspection")
	if page >= 1 {
		query, _, _ := t.Select(g.COUNT(1)).Where(ex).ToSQL()

		err := meta.MerchantDB.Get(&data.T, query)
		if err != nil {
			return data, pushLog(fmt.Errorf("%s,[%s]", err.Error(), query), "数据库错误")
		}

		if data.T == 0 {
			return data, nil
		}
	}

	offset := (page - 1) * pageSize
	query, _, _ := t.Select(colsPromoInspection...).Where(ex).Offset(offset).Limit(pageSize).Order(g.C("id").Desc()).ToSQL()

	err := meta.MerchantDB.Select(&data.D, query)
	if err != nil && err != sql.ErrNoRows {
		return data, pushLog(fmt.Errorf("%s,[%s]", err.Error(), query), "数据库错误")
	}

	data.S = pageSize
	return data, nil
}
